package com.szyjjk.service;

import com.github.pagehelper.PageHelper;
import com.szyjjk.entity.*;
import com.szyjjk.mapper.*;
import com.szyjjk.util.DateUtil;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
@Transactional(readOnly = true)
public class RunFilePathrService {
	@Autowired
	private RunFilePathMapper runFilePathMapper;

	/**
	 * 插入数据
	 * 
	 * @param runFilePath
	 * @return
	 */
	@Transactional(readOnly = false, propagation = Propagation.REQUIRED)
	public void insertRunFilePath(RunFilePath runFilePath) {
		try {	
			runFilePathMapper.insertSelective(runFilePath);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}



	/**
	 * @Author yangyue
	 * @Description //TODO 根据后缀查询生成表
	 * @Date 11:53 2020/5/22
	 * @Param [hz]
	 * @return java.util.List<com.szyjjk.entity.RunFilePath>
	 **/
	public List<RunFilePath> findFilePathByHz(String hz){
		RunFilePathExample example=new RunFilePathExample();
		example.createCriteria().andFileHzEqualTo(hz);
		example.setOrderByClause("create_time desc ");
		return this.runFilePathMapper.selectByExample(example);
	}
	/**
	 * @Author yangyue
	 * @Description //TODO 根据名字找
	 * @Date 16:35 2020/9/28
	 * @Param [name]
	 * @return java.util.List<com.szyjjk.entity.RunFilePath>
	 **/
	public List<RunFilePath> findFilePathByName(String name){
		RunFilePathExample example=new RunFilePathExample();
		example.createCriteria().andFileNameEqualTo(name); 
		return this.runFilePathMapper.selectByExample(example);
	}


	public RunFilePath findById(String mapId) {
		try {
			return runFilePathMapper.selectByPrimaryKey(mapId);	
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}


	/**
	 * @Author yangyue
	 * @Description //TODO 查询最新的一个file_sour
	 * @Date 14:07 2020/12/4
	 * @Param [name]
	 * @return com.szyjjk.entity.RunFilePath
	 **/
	public RunFilePath findByLikSOURTop1(String fileSour,String fileSoureTwo){
		RunFilePathExample example=new RunFilePathExample();
		example.createCriteria().andFileHzEqualTo("his").andFileSourEqualTo(fileSour);

		example.or(example.createCriteria().andFileHzEqualTo("his").andFileSourEqualTo(fileSoureTwo));

		example.setOrderByClause("create_time desc LIMIT 1");
		List<RunFilePath> runFilePathList=this.runFilePathMapper.selectByExample(example);
		if(runFilePathList.size()>0){
			return  runFilePathList.get(0);
		}else{

		}
		return null;
	}

	public List<RunFilePath> findByMap() {
		RunFilePathExample ex = new RunFilePathExample();
		ex.createCriteria().andFileHzEqualTo("map").andFileNameLike("%oneM.map%");
		try {
			List<RunFilePath> selectByExample = runFilePathMapper.selectByExample(ex);
			return selectByExample;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	public List<RunFilePath> findByMapAll() {
		RunFilePathExample ex = new RunFilePathExample();
		//ex.createCriteria().andFileHzEqualTo("map").andFileNameLike("%oneM.map%");
		try {
			List<RunFilePath> selectByExample = runFilePathMapper.selectByExample(ex);
			return selectByExample;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	public List<RunFilePath> findByHis() {
		RunFilePathExample ex = new RunFilePathExample();
		ex.createCriteria().andFileHzEqualTo("his").andFileNameLike("%oneM.his%");;
		try {
			List<RunFilePath> selectByExample = runFilePathMapper.selectByExample(ex);
			return selectByExample;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	@Transactional(readOnly = false, propagation = Propagation.REQUIRED)
	public void delById(String id) {
		try {
			runFilePathMapper.deleteByPrimaryKey(id);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}



	public RunFilePath findByFileName(String string) {
		RunFilePathExample ex = new RunFilePathExample();
		ex.createCriteria().andFileNameLike(string);
		List<RunFilePath> selectByExample = runFilePathMapper.selectByExample(ex);
		if(CollectionUtils.isEmpty(selectByExample)) {
			return new RunFilePath();
		}
		return selectByExample.get(0);
		
	}





}
